﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using Domain.Entity;
using Domain.Entity.bs;
using Domain.Entity.zct;
using Domain.IRepository;

namespace Application.SlotServic
{
    public class SlotService : ISlotService
    {
        private readonly ISlotRepository slotRepository;
        private readonly IMapper mapper;
        public SlotService (ISlotRepository slotRepository, IMapper mapper)
        {
            this.slotRepository = slotRepository;
            this.mapper=mapper;
        }

        //添加库位信息
        public async Task<int> AddSlot(List<AddSlotDto> addSlotDtos)
        {
            if (addSlotDtos == null || addSlotDtos.Count == 0)
                return 0;

            var slots = mapper.Map<List<Slot>>(addSlotDtos);

            // 确保每个库位都有仓库ID
            if (slots.Any(s => s.wh_id == 0))
            {
                throw new ArgumentException("仓库ID不能为空");
            }

            return await slotRepository.AddSlot(slots);
        }

        // 保持单个添加的方法（如果需要）
        public async Task<int> AddSlot(AddSlotDto addSlotDto)
        {
            var slot = mapper.Map<Slot>(addSlotDto);
            return await slotRepository.AddSlot(new List<Slot> { slot }); // 复用批量方法
        }



        //库位id删除
        public async Task<int> DeleteSlot(int id)
        {
            return await slotRepository.DeleteSlot(id);
        }

        public async Task<List<User>> GetAllUsers()
        {
            return await slotRepository.GetAllUsers(); 
        }

        //分页查询库位信息


        public async Task<(List<Slot> List, int TotalCount)> GetSlots(int pageSize, int currentPage, string? code, string? name, int? status, string? userName, int? warehouseId = null)
        {
            return await slotRepository.GetSlots(pageSize, currentPage, code, name, status, userName, warehouseId);
        }

        //根据仓库id查询对应库位信息
        public async Task<List<Slot>> GetSlotsByWhid(int whid)
        {
            return await slotRepository.GetSlotsByWhid(whid);
        }

        //修改库位
        public async Task<int> UpdateSlot(UpdateSlotDto updateSlotDto)
        {
           //转换
           Slot slot = mapper.Map<Slot>(updateSlotDto);
           return await slotRepository.UpdateSlot(slot);
        }
    }
}
